package com.yiguo.logistics.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yiguo.common.LinearLayoutAdapter;
import com.yiguo.common.LinearLayoutForListView;
import com.yiguo.logistics.entity.model.ModCommodity;
import com.yiguo.logistics.entity.model.ModOrder;
import com.yiguo.logistics.entity.model.ModPayType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

public class ActOrderDetails extends Activity
{
   ModOrder modOrder;
   
   private LinearLayoutForListView layListView;
   private LinearLayoutForListView layListViewPayType;
   private TextView codeTextView;
   private TextView sNumTextView;
   private TextView stateTextView;
   private TextView dateTextView;
   private TextView money1TextView;
   private TextView customerTextView;
   private TextView addressTextView;
//   private TextView paytypeTextView;
//   private TextView money2TextView;

   
   
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  // TODO Auto-generated method stub
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.act_tmp_orderdetails);
	  
	  layListView=(LinearLayoutForListView)findViewById(R.id.layListView_Act_orderdetails);
	  layListViewPayType=(LinearLayoutForListView)findViewById(R.id.layListViewPayType_Act_orderdetails);
	  codeTextView=(TextView)findViewById(R.id.txtOrderCode_Act_orderdetails);
	  sNumTextView=(TextView)findViewById(R.id.txtSNumber_Act_orderdetails);
	  stateTextView=(TextView)findViewById(R.id.txtOrderState_Act_orderdetails);
	  dateTextView=(TextView)findViewById(R.id.txtOrderDate_Act_orderdetails);
	  money1TextView=(TextView)findViewById(R.id.txtAmount_Act_orderdetails);
	  customerTextView=(TextView)findViewById(R.id.txtCustomer_Act_orderdetails);
	  addressTextView=(TextView)findViewById(R.id.txtAddress_Act_orderdetails);
//	  paytypeTextView=(TextView)findViewById(R.id.txtPayType_Act_orderdetails);
//	  money2TextView=(TextView)findViewById(R.id.txtOrderAmount2_Act_orderdetails);
	  
	  modOrder=(ModOrder)getIntent().getSerializableExtra("modOrder");
	  
	  LinearLayoutAdapter cAdapter=new LinearLayoutAdapter(ActOrderDetails.this, 
			   getAllCommodity(), 
			   R.layout.listview_orderdetail_list, 
			   new String[] { "name","num","amount" },
			   new int[] { R.id.txtName_listview_orderdetails,R.id.txtNum__listview_orderdetails,R.id.txtAmount_listview_orderdetails });
	  layListView.setAdapter(cAdapter);
	 
	  LinearLayoutAdapter pAdapter=new LinearLayoutAdapter(ActOrderDetails.this, 
			getAllPayType(), 
			R.layout.listview_orderdetail_paytype, 
			new String[] { "name","amount" },
			new int[]{R.id.txtPayType_listview_orderdetails,R.id.txtPayTypeMoney_listview_orderdetails});
	  layListViewPayType.setAdapter(pAdapter);
	  
	  codeTextView.setText(modOrder.getOrderCode());
	  sNumTextView.setText(modOrder.getSerialNumber());
	  stateTextView.setText(modOrder.getOrderState());
	  dateTextView.setText(modOrder.getDeliveryDate());
	  money1TextView.setText(modOrder.getTotalPrice());
	  customerTextView.setText(modOrder.getConsignee());
	  addressTextView.setText(modOrder.getAddress());
//	  paytypeTextView.setText(modOrder.getPaymentType());
//	  money2TextView.setText(modOrder.getPaymnetPrice());
	  
//	  UIHelper.setListViewHeightBasedOnChildren(listView);
   }

   
   
   /**
    * 加载所有商品
    * 
    * @return
    */
   public List<Map<String, Object>> getAllCommodity()
   {
	  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	  Map<String, Object> map = null;
	  for (ModCommodity cc : modOrder.getCommodityList())
	  {
		 map = new HashMap<String, Object>();
		 map.put("name", cc.getCommodityName());
		 map.put("num", cc.getCommodityAmount());
		 map.put("amount",cc.getOrderTotalPrice());
		 list.add(map);
	  }
	  return list;
   }
   
   /**
    * 加载所有支付方式
    * 
    * @return
    */
   public List<Map<String, Object>> getAllPayType()
   {
	  List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	  Map<String, Object> map = null;
	  for (ModPayType cc : modOrder.getPayTypeList())
	  {
		 map = new HashMap<String, Object>();
		 map.put("name", cc.getPayType());
		 map.put("amount",cc.getPayAmount());
		 list.add(map);
	  }
	  return list;
   }
   
   
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
	  { // 按下的如果是BACK，同时没有重复
		 Intent intent=new Intent();
		 intent.putExtra("order", modOrder);
		 setResult(RESULT_OK, intent);
		 this.finish();
	  }
	  return false;
   }
}
